package com.caipin.service.impl;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-10-06 12:10
 * @Description: TODO
 */
@Service
public class HomePageManagementServiceImpl implements HomePageManagementService {
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private GateAttributeDao gateAttributeDao;

    @Autowired
    private GateComparisonResultDao gateComparisonResultDao;

    @Autowired
    private SecurityFaceDao securityFaceDao;

    @Autowired
    private SecurityImageDao securityImageDao;

    @Autowired
    private CourseEventReportDao courseEventReportDao;

    public PopulationCount getPopulationSize() {
        Integer studentCount = this.studentDao.selectCount(null);
        Integer teacherCount = this.teacherDao.selectCount(null);
        PopulationCount populationCount = (new PopulationCount()).setStudent(studentCount).setTeacher(teacherCount).setTotal(Integer.valueOf(studentCount.intValue() + teacherCount.intValue()));
        return populationCount;
    }

    public TemperatureInfo getTemperatureInfo() {
        try {
            QueryWrapper<GateComparisonResult> queryWrapper = new QueryWrapper();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = simpleDateFormat.format(new Date());
            String startDate = date + " 00:00:00";
            String endDate = date + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long startTime = sdf.parse(startDate).getTime() / 1000L * 1000L;
            long endTime = sdf.parse(endDate).getTime() / 1000L * 1000L;
            queryWrapper.ge("screen_time", startDate);
            queryWrapper.le("screen_time", endDate);
            List<GateComparisonResult> gateComparisonResultList = this.gateComparisonResultDao.selectList((Wrapper)queryWrapper);
            QueryWrapper<GateAttribute> wrapper = new QueryWrapper();
            wrapper.ge("screen_time", Long.valueOf(startTime));
            wrapper.le("screen_time", Long.valueOf(endTime));
            List<GateAttribute> gateAttributeList = this.gateAttributeDao.selectList((Wrapper)wrapper);
            int normalPeople = 0;
            int abnormalPeople = 0;
            for (GateAttribute gateAttribute : gateAttributeList) {
                Float value = Float.valueOf(Float.parseFloat(gateAttribute.getTemperature()));
                if (value != null) {
                    if (value.floatValue() > 37.2F) {
                        abnormalPeople++;
                        continue;
                    }
                    normalPeople++;
                }
            }
            for (GateComparisonResult gateComparisonResult : gateComparisonResultList) {
                Float value = Float.valueOf(Float.parseFloat(gateComparisonResult.getTemperature()));
                if (value != null) {
                    if (value.floatValue() > 37.2F) {
                        abnormalPeople++;
                        continue;
                    }
                    normalPeople++;
                }
            }
            TemperatureInfo temperatureInfo = (new TemperatureInfo()).setTotal(normalPeople + abnormalPeople).setAbnormal(abnormalPeople).setNormal(normalPeople);
            return temperatureInfo;
        } catch (Throwable $ex) {
            throw $ex;
        }
    }

    public List<VisitInfo> getNumberOfPeopleTakingVisitRotationChart() {
        QueryWrapper<SecurityFace> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mark", Boolean.valueOf(false));
        queryWrapper.eq("face_stranger", Boolean.valueOf(true));
        queryWrapper.ne("image_id", Integer.valueOf(0));
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 10");
        List<SecurityFace> securityFaceList = this.securityFaceDao.selectList((Wrapper)queryWrapper);
        List<VisitInfo> visitInfoList = new ArrayList<>();
        for (SecurityFace securityFace : securityFaceList) {
            SecurityImage securityImage = (SecurityImage)this.securityImageDao.selectById(securityFace.getImageId());
            String securityFacePath = securityFace.getImageSrc().replaceFirst("^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+:[0-9]+", "");
            String securityImagePath = securityImage.getImageSrc().replaceFirst("^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+:[0-9]+", "");
            visitInfoList.add((new VisitInfo()).setImageSrc(securityFacePath)
                    .setTime(securityFace.getSnapTime())
                    .setParentSrc(securityImagePath)
                    .setLocation(securityImage.getLocation()));
        }
        return visitInfoList;
    }

    public List<ClassroomEvent> getNumberOfPeopleTakingClassroom(String startDate, String endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end = null;
        try {
            start = simpleDateFormat.parse(startDate + " 00:00:00");
            end = simpleDateFormat.parse(endDate + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        queryWrapper.ge("create_time", start);
        queryWrapper.le("create_time", end);
        List<CourseEventReport> courseEventReportList = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport courseEventReport : courseEventReportList) {
            String action = courseEventReport.getAction();
            if (map.get(action) == null) {
                map.put(action, Integer.valueOf(1));
                continue;
            }
            map.put(action, Integer.valueOf(((Integer)map.get(action)).intValue() + 1));
        }
        List<ClassroomEvent> classroomEventList = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            ClassroomEvent classroomEvent = (new ClassroomEvent()).setEvent(entry.getKey()).setCount(((Integer)entry.getValue()).intValue());
            classroomEventList.add(classroomEvent);
        }
        return classroomEventList;
    }

    public List<InAndOutInfo> getNumberOfPeopleTakingIn(String startDate, String endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        try {
            start = simpleDateFormat.parse(startDate);
            end = simpleDateFormat.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(new String[] { "COUNT(0) as c", "DATE(create_time) as date" });
        queryWrapper.ge("create_time", start);
        queryWrapper.le("create_time", end);
        queryWrapper.groupBy("DATE(create_time)");
        List list = this.gateAttributeDao.selectMaps((Wrapper)queryWrapper);
        List<InAndOutInfo> inAndOutInfoList = new ArrayList<>();
        for (Object o : list) {
            Map<String, Object> map = (Map<String, Object>)o;
            inAndOutInfoList.add((new InAndOutInfo())

                    .setOut(0L)
                    .setIn(((Long)map.get("c")).longValue())
                    .setDate((Date)map.get("date")));
        }
        return inAndOutInfoList;
    }

    public List<BodyTemperatureReport> getRealtimeReportOfBodyTemperature(Integer count) {
        QueryWrapper<GateComparisonResult> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.orderByDesc("screen_time"))
                .last("LIMIT " + count);
        List<GateComparisonResult> gateComparisonResultList = this.gateComparisonResultDao.selectList((Wrapper)queryWrapper);
        List<BodyTemperatureReport> bodyTemperatureReportList = new ArrayList<>();
        for (GateComparisonResult gateComparisonResult : gateComparisonResultList)
            bodyTemperatureReportList.add((new BodyTemperatureReport())

                    .setTemperature(gateComparisonResult.getTemperature())
                    .setUsername(gateComparisonResult.getUsername()));
        return bodyTemperatureReportList;
    }

    public String getPanoramaCapturedByStrangers(Long parentId) {
        SecurityImage securityImage = (SecurityImage)this.securityImageDao.selectById(parentId);
        if (securityImage == null)
            return null;
        return securityImage.getImageSrc();
    }
}

